﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Forget.HRBLL.Logical;
using Forget.HRBLL.Model;

namespace HRWebSite.Employee
{
    public partial class EditEmployee : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            this.HRLimits = Limits.EmployeeManage;
            this.HRLimitAttribute = Forget.HRBLL.LimitAttribute.Read | Forget.HRBLL.LimitAttribute.Modify;
            base.Page_Load(sender, e);
            if (!this.IsPostBack)
            {
                if (!string.IsNullOrEmpty(this.Request["NumID"]))
                    this.DatabindView(this.Request["NumID"]);
                else
                    this.Response.Write("<script>alert('传入参数有误，请重试或与管理员联系！');</script>");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            DateTime m_birthday = new DateTime(int.Parse(this.Year1.SelectedValue), int.Parse(this.Month1.SelectedValue), int.Parse(this.Day1.SelectedValue));
            DateTime m_intim = new DateTime(int.Parse(this.Year2.SelectedValue), int.Parse(this.Month2.SelectedValue), int.Parse(this.Day2.SelectedValue));
            float m_height = string.IsNullOrEmpty(this.Height.Text) ? 0 : long.Parse(this.Height.Text);
            float m_weight = string.IsNullOrEmpty(this.Weight.Text) ? 0 : long.Parse(this.Weight.Text);
            int m_eptype = int.Parse(this.EpType.SelectedValue);
            int m_positionnum = int.Parse(this.DropDownList8.SelectedValue);
            bool sex = this.Sex.SelectedValue == "0" ? true : false;
            bool marry = this.Marry.SelectedValue == "1" ? false : true;
            int depart = 0;
            int.TryParse(this.Request["departID"], out depart);
            if (EmployeeLogical.ModifyEmployeeInfo(this.NumberID.Text, sex, m_birthday, m_height, m_weight, this.Party.Text,
                this.Place.Text, m_intim, m_eptype, this.IDCard.Text, this.Address.Text, this.Education.Text,
                this.Professional.Text, this.Nation.Text, m_positionnum, marry, this.University.Text, depart))
            {
                Response.Write("<script>alert('修改成功');</script>");
                this.Literal1.Text = string.Format("<input type='text' id='departName' name='departName' onclick='Showdepart()' value='{0}' readonly='readonly' /> <input id='departID' name='departID' value='{1}' type='hidden' />", this.Request.Params["departName"], depart);
            }
            else
                Response.Write("<script>alert('修改失败');</script>");
        }

        private void DatabindView(string numberid)
        {
            EmployeeInfo eInfo = EmployeeLogical.GetInfo_NumberID(numberid);
            if (eInfo != null)
            {
                if (eInfo.Birthday.Year >= 1942 && eInfo.Birthday.Year <= 2002)
                {
                    this.Year1.SelectedValue = eInfo.Birthday.Year.ToString();
                    this.Month1.SelectedValue = eInfo.Birthday.Month.ToString();
                    this.Day1.SelectedValue = eInfo.Birthday.Day.ToString();
                }

                if (eInfo.InTime.Year >= 2000 && eInfo.InTime.Year <= 2012)
                {
                    this.Year2.SelectedValue = eInfo.InTime.Year.ToString();
                    this.Month2.SelectedValue = eInfo.InTime.Month.ToString();
                    this.Day2.SelectedValue = eInfo.InTime.Day.ToString();
                }

                this.NumberID.Text = eInfo.NumberID;
                this.Sex.SelectedValue = eInfo.Sex == true ? "0" : "1";
                this.Height.Text = eInfo.Height == null ? "" : eInfo.Height.ToString();
                this.Weight.Text = eInfo.Weight == null ? "" : eInfo.Weight.ToString();
                this.Party.Text = string.IsNullOrEmpty(eInfo.Party) ? "" : eInfo.Party;
                this.Place.Text = string.IsNullOrEmpty(eInfo.Place) ? "" : eInfo.Place;
                if (eInfo.EpType != null)
                    this.EpType.SelectedValue = eInfo.EpType.ToString();
                this.IDCard.Text = eInfo.IDCard;
                this.Address.Text = string.IsNullOrEmpty(eInfo.Address) ? "" : eInfo.Address;
                this.Education.Text = string.IsNullOrEmpty(eInfo.Education) ? "" : eInfo.Education;
                this.Professional.Text = string.IsNullOrEmpty(eInfo.Profession) ? "" : eInfo.Profession;
                this.Nation.Text = string.IsNullOrEmpty(eInfo.Nation) ? "" : eInfo.Nation;
                this.University.Text = string.IsNullOrEmpty(eInfo.University) ? "" : eInfo.University;
                if (eInfo.PositionNum != null)
                    this.DropDownList8.SelectedValue = eInfo.PositionNum.ToString();
                this.Marry.SelectedValue = eInfo.Marry == true ? "2" : "1";
                Department dp = DepartmentLogical.GetDepart_ID(eInfo.DpartNum);
                if (dp != null)
                    this.Literal1.Text = string.Format("<input type='text' id='departName' name='departName' onclick='Showdepart()' value='{0}' readonly='readonly' /> <input id='departID' name='departID' value='{1}' type='hidden' />", dp.Name, dp.DepartmentID);
                else
                    this.Literal1.Text = string.Format("<input type='text' id='departName' name='departName' onclick='Showdepart()' readonly='readonly' /> <input id='departID' name='departID' type='hidden' />");
            }
            else
                Response.Write("<script>alert('传入的参数有误，请重试或与系统管理员联系！')</script>");
        }
    }
}